import { _decorator, Component, find, Node } from 'cc';
import { QipanController } from './QipanController';
import PieceManagerImpl from './PieceManagerImpl';
import { NodeController } from './NodeController';
import { GameManager } from './GameManager';
const { ccclass, property } = _decorator;
@ccclass('PieceManagerBase')
export class PieceManagerBase extends Component implements PieceManagerImpl {
    gameManager: GameManager;
    canMoveOrAttck(): Promise<boolean> {
        throw new Error('Method not implemented.');
    }
    showAllowedRoute(event: any): any {
        if (this.isEnemy) return false;
        if (this.getGameManager().currentPiece && this.getGameManager().currentPiece == this) {
            this.getGameManager().clearMoveOrEatNode()
            return false;
        }
        if (this.getGameManager().currentPiece && this.getGameManager().currentPiece != this) {
            this.getGameManager().clearMoveOrEatNode()
            this.getGameManager().currentPiece = this;
            return true;
        }
        this.getGameManager().currentPiece = this;
        return true;
    }
    @property
    isEnemy: boolean = false;
    @property
    posX: number;
    @property
    posY: number;
    @property
    pieceName: string;
    qipanController: QipanController;

    start() {
        this.node.on(Node.EventType.TOUCH_START, this.showAllowedRoute, this)
    }

    getQipanController() {
        if (!this.qipanController) {
            this.qipanController = find("Canvas/棋盘/qipanBg").getComponent(QipanController);
        }
        return this.qipanController;
    }
    getGameManager() {
        if (!this.gameManager) {
            this.gameManager = find('Canvas/GameManager').getComponent(GameManager);
        }
        return this.gameManager;
    }

    initPiece() {
        let node = this.getQipanController().getNode(this.posX, this.posY)
        node.getComponent(NodeController).ownNode(this);
        this.node.setPosition(node.getPosition());
    }
    freeNode() {
        let node = this.getQipanController().getNode(this.posX, this.posY)
        node.getComponent(NodeController).clearNode();
    }

    update(deltaTime: number) {

    }
}

